package com.uenpay.im.ui.old.activity.funds;

import com.qiyunxin.android.http.net.HttpRequestType;
import com.uenpay.im.invokeitems.HttpRequestHandle;

import java.util.ArrayList;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * @E-mail:
 * @date 创建时间：2016-5-10 下午7:03:24
 * @version 1.0
 * @function 经费管理
 */
public class FundsHandle {

	public interface IFundsListener {
		void onFundsResult(int status, String msg);
	}

	/**
	 * 添加群经费记录
	 * 
	 * @param chatid
	 * @param custid
	 *            收款对象用户id(可为空)
	 * @param amount
	 * @param content
	 * @param direction
	 *            收支(1为收入，2为支出)
	 */
	public static void addFunds(String chatid, String custid, String amount,
			String content, int direction, final IFundsListener mIFundsListener) {
		AddChatFeeTurnoverInvokeItem item = new AddChatFeeTurnoverInvokeItem(
				chatid, custid, amount, content, direction);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
										 String failReason) {
						if (resultObject != null) {
							AddChatFeeTurnoverInvokeItem.AddChatFeeTurnoverInvokeItemResult result = ((AddChatFeeTurnoverInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mIFundsListener.onFundsResult(result.status,
										result.msg);
							} else {
								mIFundsListener.onFundsResult(-1, "");
							}
						} else {
							mIFundsListener.onFundsResult(-1, failReason);
						}
					}
				});

	}

	/**
	 * 群成员缴纳经费
	 * 
	 * @param contributid
	 * @param paypassword
	 * @param mIFundsListener
	 */
	public static void doContributToGroupFunds(String contributid,
			String paypassword, final IFundsListener mIFundsListener) {
		DoContributInvokeItem item = new DoContributInvokeItem(contributid,
				paypassword);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
										 String failReason) {
						if (resultObject != null) {

							DoContributInvokeItem.DoContributInvokeItemResult result = ((DoContributInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mIFundsListener.onFundsResult(result.status,
										result.msg);
							} else {
								mIFundsListener.onFundsResult(-1, "");
							}
						} else {
							mIFundsListener.onFundsResult(-1, failReason);
						}
					}
				});
	}

	public interface IGetFundsRevenueListener {
		void onGetFundsRevenueResult(
				GetChatRevenueListInvokeItem.GetChatRevenueListInvokeItemResult result);
	}

	/**
	 * 获取收入列表
	 * 
	 * @param chatid
	 * @param mIGetFundsRevenueListener
	 */
	public static void getFundsRevenueList(String chatid,
			final IGetFundsRevenueListener mIGetFundsRevenueListener) {
		GetChatRevenueListInvokeItem item = new GetChatRevenueListInvokeItem(
				chatid);
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {

							GetChatRevenueListInvokeItem.GetChatRevenueListInvokeItemResult result = ((GetChatRevenueListInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mIGetFundsRevenueListener
										.onGetFundsRevenueResult(result);
							} else {
								mIGetFundsRevenueListener
										.onGetFundsRevenueResult(null);
							}
						} else {
							mIGetFundsRevenueListener
									.onGetFundsRevenueResult(null);
						}
					}
				});
	}

	/**
	 * 交经费
	 * 
	 * @param chatid
	 * @param amount
	 * @param mIFundsListener
	 */
	public static void deliveryFunds(String chatid, String amount,
			final IFundsListener mIFundsListener) {
		DeliveryFundsInvokeItem item = new DeliveryFundsInvokeItem(chatid,
				amount);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {

							DeliveryFundsInvokeItem.DeliveryFundsInvokeItemResult result = ((DeliveryFundsInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mIFundsListener.onFundsResult(result.status,
										result.msg);
							} else {
								mIFundsListener.onFundsResult(-1, failReason);
							}
						} else {
							mIFundsListener.onFundsResult(-1, failReason);
						}
					}
				});
	}

	public interface IGetChatFundsAmount {
		void onGetFundsAmount(
				final GetFundsChatAmountInvokeItem.GetFundsChatAmountInvokeItemResult result);
	}

	/**
	 * 获取经费群余额信息
	 * 
	 * @param chatid
	 * @param mIGetChatFundsAmount
	 */
	public static void getChatFundsAmount(String chatid,
			final IGetChatFundsAmount mIGetChatFundsAmount) {
		GetFundsChatAmountInvokeItem item = new GetFundsChatAmountInvokeItem(
				chatid);
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							GetFundsChatAmountInvokeItem.GetFundsChatAmountInvokeItemResult result = ((GetFundsChatAmountInvokeItem) resultObject)
									.getOutPut();
							mIGetChatFundsAmount.onGetFundsAmount(result);
						} else {
							mIGetChatFundsAmount.onGetFundsAmount(null);
						}
					}
				});
	}

	/**
	 * 删除经费支出
	 * 
	 * @param id
	 * @param chatid
	 * @param mIFundsListener
	 */
	public static void deleteFundsCost(int id, String chatid,
			final IFundsListener mIFundsListener) {
		DeleteFundsCostInvokeItem item = new DeleteFundsCostInvokeItem(id,
				chatid);
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							DeleteFundsCostInvokeItem.DeleteFundsCostInvokeItemResult result = ((DeleteFundsCostInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mIFundsListener.onFundsResult(result.status,
										result.msg);
							} else {
								mIFundsListener.onFundsResult(-1, failReason);
							}
						} else {
							mIFundsListener.onFundsResult(-1, failReason);
						}
					}
				});
	}

	public interface IGetFundsListListener {
		void onGetFundsResult(int status, String msg,
											  ArrayList<FundsOutEntity> arrayList);
	}

	/**
	 * 获取群经费支出收入
	 * 
	 * @param chatid
	 * @param direction
	 */
	public static void getFundsList(String chatid, int direction,
			final IGetFundsListListener mIGetFundsListListener) {
		GetChatFeeTurnoverInvokeItem item = new GetChatFeeTurnoverInvokeItem(
				chatid, direction);

		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							GetChatFeeTurnoverInvokeItem.GetChatFeeTurnoverInvokeItemResult result = ((GetChatFeeTurnoverInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mIGetFundsListListener.onGetFundsResult(
										result.status, result.msg,
										result.arrayList);
							} else {
								mIGetFundsListListener.onGetFundsResult(-1, "",
										null);
							}

						} else {
							mIGetFundsListListener.onGetFundsResult(-1,
									failReason, null);
						}
					}
				});

	}

	/**
	 * 设置入群经费
	 * 
	 * @param chatid
	 * @param amount
	 * @param mIFundsListener
	 */
	public static void setGroupFundsAmount(String chatid, String amount,
			final IFundsListener mIFundsListener) {
		SetChatAmountInvokeItem item = new SetChatAmountInvokeItem(chatid,
				amount);

		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							SetChatAmountInvokeItem.SetChatAmountInvokeItemResult result = ((SetChatAmountInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mIFundsListener.onFundsResult(result.status,
										result.msg);
							} else {
								mIFundsListener.onFundsResult(-1, failReason);
							}

						} else {
							mIFundsListener.onFundsResult(-1, failReason);
						}
					}
				});
	}
}
